dojo.provide("xray.Updater");

dojo.registerModulePath("xray.images", "../../images");
dojo.requireLocalization("xray", "Updater");

xray.Updater = {
	currentVersion: "1.0",
	updateUrl: "http://xray.uxebu.com/air/public/version/xray.version",
	messages: dojo.i18n.getLocalization("xray", "Updater", this.lang),
	appendTo: "contentCenter",
	id: "xray_Updater",
	
	contentConfirmUpdate: '<fieldset class="updaterContainer">'+
						'<legend>${confirmUpdate}</legend>'+
						'<p class="updaterTitle">${releaseInfo}</p>'+
						'<ul id="${id}_updateInfo"></ul>'+
						'<p><span id="${id}_installUpdate"><img src="${btnInstall}" class="formSubmit"></span> <span id="${id}_cancelUpdate"><img src="${btnCancel}" class="formSubmit"></span></p>'+
					'</fieldset>',
	
	checkForUpdate: function(){
		// some initial setup
		this.messages.id = this.id;
		
		this.updReq = new air.URLRequest(this.updateUrl);
		this.updStream = new air.URLStream();
		this.updData = new air.ByteArray();
		
		// initialize message
		dojo.publish("xray/tools/recordingstats/event", [{type: this.messages.updater, log: this.messages.updateCheck}]);
		
		this.updStream.addEventListener(air.Event.COMPLETE, dojo.hitch(this, function(){
			this.updStream.readBytes(this.updData, 0, this.updStream.bytesAvailable);
	
			var data = dojo.fromJson(this.updData);
			if (!data.version || !data.url){
				dojo.publish("xray/tools/recordingstats/event", [{type: this.messages.updater, log: this.messages.updateFalseFormat}]);
			}else if (parseFloat(data.version) <= parseFloat(this.currentVersion)){
				dojo.publish("xray/tools/recordingstats/event", [{type: this.messages.updater, log: this.messages.updateNo}]);
			}else{
				this.updateVersion = data.version;
				this.updateUrl = data.url;
				
				this.messages.confirmUpdate = dojo.string.substitute(this.messages.confirmUpdate, {version: this.updateVersion});			
				if (!this.dialogUpdate){				
					this.dialogUpdate = new xray.Dialog({
						draggable: false, 
						titleBar: false, 
						content: dojo.string.substitute(this.contentConfirmUpdate, this.messages),
					});
					this.dialogUpdate.startup();
					
					dojo.connect(dojo.byId(this.id+"_installUpdate"), "onclick", this, "_runUpdate");
					dojo.connect(dojo.byId(this.id+"_cancelUpdate"), "onclick", this, "_cancelUpdate");	
				}
				if (data.notes){
					dojo.byId(this.id+"_updateInfo").innerHTML = data.notes;					
				}else {
					dojo.byId(this.id+"_updateInfo").innerHTML = this.messages.noReleaseInfo;
				}
				
				this.dialogUpdate.show();		
			}
		}));
		
		this.updStream.addEventListener(air.IOErrorEvent.IO_ERROR, dojo.hitch(this, function(){
			dojo.publish("xray/tools/recordingstats/event", [{type: this.messages.updater, log: this.messages.updateError}]);
		}));	
		
		this.updStream.load(this.updReq);
	},
	
	_cancelUpdate: function(){
		this.dialogUpdate.hide();
		
		dojo.publish("xray/tools/recordingstats/event", [{type: this.messages.updater, log: this.messages.updateCancelled}]);
	},
	
	_runUpdate: function(){			
		this.dialogUpdate.hide();
			
		dojo.publish("xray/tools/recordingstats/event", [{type: this.messages.updater, log: dojo.string.substitute(this.messages.updateDownloading, {url: this.updateUrl})}]);
		
		this.urlReq = new air.URLRequest(this.updateUrl);
		this.urlStream = new air.URLStream();
		this.fileData = new air.ByteArray();
		this.urlStream.addEventListener(air.Event.COMPLETE, dojo.hitch(this, "_writeAir"));
		this.urlStream.load(this.urlReq);
	},
	
	update: function(){
		var updater = new air.Updater();
		var version = this.updateVersion;
		updater.update(this.file, this.updateVersion);
	},
	
	_writeAir: function() {
	    this.urlStream.readBytes(this.fileData, 0, this.urlStream.bytesAvailable);
		
		this.file = air.File.desktopDirectory.resolvePath("xRayUpdate.air");
	    var fileStream = new air.FileStream();
	    fileStream.open(this.file, air.FileMode.WRITE);
	    fileStream.writeBytes(this.fileData, 0, this.fileData.length);
	    fileStream.close();
	    
		this.update();
	}
	
};